<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <style>
    #dv {
        width: 200px;
        height: 600px;
        background-color: aquamarine;
        border: 1px solid red;
    }
    ul {
        list-style-type: none;
    }

    ul li {
        cursor: pointer;
    }
    
    </style>
    <title>Create list by the 3rd element creating method.</title>
</head>
<body>
    <input type="button" value="Click" id="btn">
    <div id="dv"></div>

    <script>
        document.getElementById("btn").onclick = function(){
            var names = ["郭靖", "杨过", "张无忌", "令狐冲", "韦小宝", "张三丰"];
            var ulObj = document.createElement("ul");
            document.getElementById("dv").appendChild(ulObj);

            for (i=0; i<names.length; i++){
                var liObj = document.createElement("li");
                liObj.innerText = names[i];
                ulObj.appendChild(liObj);

                liObj.onmouseover = mouseoverHandler;
                liObj.onmouseout = mouseoutHandler;
            }
        }
    
    // Functions are out of the clicking event to avoid waste space.
    function mouseoverHandler(){
        this.style.backgroundColor = "yellow";
    }
    function mouseoutHandler(){
        this.style.backgroundColor = "";
    }
    
    </script>
</body>
</html>